package com.etc.controller;


import com.etc.entity.Goods;
import com.etc.service.GoodsService;
import com.etc.service.impl.GoodsServiceImpl;
import com.etc.util.PageData;
import com.etc.util.QueryResponseData;
import org.apache.commons.logging.LogFactory;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//通过后端来处理跨域问题
@RestController
@CrossOrigin
@RequestMapping("api")
//@CrossOrigin(origins = "*",allowCredentials="true",allowedHeaders = "",methods = {})
public class GoodsController {
    //创建一个对象
//    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

    Logger logger = LogManager.getLogger(getClass());

    @Autowired
    private GoodsService goodsService ;

    /**
     * 这里的方法暂时先这样
     * 后面要根据前后交换规范json格式来修改
     * @param pageNo
     * @param pageSize
     * @param keywords
     * @return
     */
    @GetMapping("goods/")
    public QueryResponseData getByPage(@RequestParam(name = "page", required = false, defaultValue = "1") int pageNo, @RequestParam(name = "limit", required = false, defaultValue = "10") int pageSize, @RequestParam(name = "keywords", required = false, defaultValue = "") String keywords) {
        PageData pageData = goodsService.getByPage(pageNo, pageSize, keywords);

        QueryResponseData<Goods> queryResponseData = new QueryResponseData<>();
        //数量
        queryResponseData.setCount(pageData.getTotal());
        queryResponseData.setData(pageData.getData());

        logger.debug("logger debug GoodsController getByPage ");
        logger.info("logger info GoodsController getByPage ");
        logger.warn("logger warn GoodsController getByPage ");
        logger.error("logger error GoodsController getByPage ");

        return queryResponseData;
    }
}
